package org.flh.book.shop.common.response;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.List;
import lombok.Data;
import org.flh.book.shop.utils.Page;

/**
 * 描述：对应 ant design pro table 查询返回的数据格式
 *
 * @author 花好月圆
 * @date 2021/1/19 16:03
 */
@Data
@ApiModel
public class ProTableResponse<T> {

    @ApiModelProperty("成功标识")
    private Boolean success;

    @ApiModelProperty("结果集")
    private List<T> data;

    /**
     * 分页信息
     * 包含 total current 以及 pageSize
     */
    @ApiModelProperty(value = "总数", example = "0")
    private Integer total;

    @ApiModelProperty(value = "当前页", example = "0")
    private Integer current;

    @ApiModelProperty(value = "每页数据条数", example = "0")
    private Integer pageSize;

    public static <T> ProTableResponse<T> buildSuccess(List<T> data, Page page, Integer total) {
        ProTableResponse<T> response = new ProTableResponse<>();
        response.setSuccess(true);
        response.setData(data);
        response.setTotal(total);
        response.setCurrent(page.getCurrentPage());
        response.setPageSize(page.getPageSize());
        return response;
    }

    public static ProTableResponse buildError() {
        ProTableResponse response = new ProTableResponse();
        response.setSuccess(false);
        return response;
    }

    private ProTableResponse() {}
}
